package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    //扩展方法:保存套餐数据 及和套餐菜品关联数据
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        //添加套餐数据
        this.save(setmealDto);
        //添加套餐菜品关联数据
        List<SetmealDish> sd = setmealDto.getSetmealDishes();
        sd.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));

        setmealDishService.saveBatch(sd);

    }

    //扩展方法L分页查询
    @Override
    public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        return setmealMapper.pageSetmealDto(setmealDtoPage, name);
    }

    //扩展方法:删除
    @Override
    public void deleteByIds(Long[] ids) {
        //查询套餐是否起售
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids).eq(Setmeal::getStatus,1);
        Integer integer = setmealMapper.selectCount(lqw);
        if (integer>0){
            throw new CustomerException("傻逼这个套餐还在卖,删不了");
        }
        setmealDishService.lambdaUpdate().in(SetmealDish::getId,ids).remove();
        setmealMapper.deleteBatchIds(Arrays.asList(ids));


    }
    //根据套餐查询信息
    @Override
    public SetmealDto getSetmealByIdWithDish(Long id) {
        SetmealDto setmealDto=setmealMapper.getSetmalByIdWithDish(id);
        return setmealDto;
    }

    //修改
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改setmeal数据
        this.updateById(setmealDto);
        //更新套餐菜品关联数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishService.lambdaUpdate()
                        .eq(SetmealDish::getSetmealId,setmealDto.getId())
                        .remove();
        //Todo 给每个setmalDish 设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);

    }


}
